/* Copyright 2008. All rights reserved. */
package org.bling.util;

import java.util.ArrayList;
import java.util.List;

import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.Tree;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * <code>Util</code> is a library of static stateless methods
 * that are utilized in multiple places within the framework.
 * 
 * @author DISBROB
 */
public abstract class Util {

	public static boolean isBlankOrNull(String str) {
		if (str == null || str.equals("")) {
			return true;
		}
		return false;
	}

	// Given a string, capitalize the first letter
	public static String capitalize(String str) {
		if (isBlankOrNull(str)) { return ""; }
		String firstLetter = str.substring(0, 1);
		String remainder = str.substring(1);
		String capitalized = firstLetter.toUpperCase() + remainder;
		return capitalized;
	}
	
	public static StringBuffer prettyPrint(CommonTree tree) {
		return prettyPrint(tree, 0);
	}
	
	public static StringBuffer prettyPrint(Tree tree, int depth) {
		StringBuffer sb = new StringBuffer();
		for (int i = 1; i <= depth; i++) {
			sb.append("\t");
		}

		sb.append(tree + "\n");

		for (int i=0; i<tree.getChildCount(); i++) {
			Tree child = tree.getChild(i);
			sb.append(prettyPrint(child, depth+1));
		}
		return sb;
	}
	
	public static List<Control> getChildrenByType(Composite composite, Class klass) {
		List<Control> retval = new ArrayList<Control>();
		
		Control[] children = composite.getChildren();
		for (int i=0; i< children.length; i++) {
			if (klass.isInstance(children[i])) {
				retval.add(children[i]);
			}
			
			if (children[i] instanceof Composite) {
				retval.addAll(getChildrenByType(((Composite)children[i]), klass));
			}
		}
		
		return retval;
	}
}
